package org.jeecg.modules.hc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Joiner;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.hc.entity.*;
import org.jeecg.modules.hc.enums.HcOrderStateEnum;
import org.jeecg.modules.hc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 企业详情表
 * @Author: jeecg-boot
 * @Date:   2023-07-04
 * @Version: V1.0
 */
@Api(tags="企业详情表")
@RestController
@RequestMapping("/hc/hcCompanyDetail")
@Slf4j
public class HcCompanyDetailController extends JeecgController<HcCompanyDetail, IHcCompanyDetailService> {
	@Autowired
	private IHcCompanyDetailService hcCompanyDetailService;
	@Autowired
	private IHcCompanyFoodDisplayService hcCompanyFoodDisplayService;

	@Autowired
	private IHcCompanyInfoService hcCompanyInfoService;
	@Autowired
	private IHcCompanyFoodOrderService hcCompanyFoodOrderService;
	@Autowired
	private IHcMerchantService hcMerchantService;
	/**
	 * 分页列表查询
	 *
	 * @param hcCompanyDetail
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "企业详情表-分页列表查询")
	@ApiOperation(value="企业详情表-分页列表查询", notes="企业详情表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<HcCompanyDetail>> queryPageList(HcCompanyDetail hcCompanyDetail,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HcCompanyDetail> queryWrapper = QueryGenerator.initQueryWrapper(hcCompanyDetail, req.getParameterMap());
		queryWrapper.orderByDesc("MENU_DISPLAY_TIME");
		Page<HcCompanyDetail> page = new Page<HcCompanyDetail>(pageNo, pageSize);
		IPage<HcCompanyDetail> pageList = hcCompanyDetailService.page(page, queryWrapper);
		List<HcCompanyDetail> records = pageList.getRecords();
		for (HcCompanyDetail record : records) {
			//获取商户id
			String listingStoreIds = record.getListingStoreIds();
			List<String> merchantNames = new ArrayList<>();
			if (listingStoreIds.contains(",")){//多个商户
				List<String> merchantIds = Arrays.asList(listingStoreIds.split(","));
				for (String merchantId : merchantIds) {
					HcMerchant one = hcMerchantService.getOne(new LambdaQueryWrapper<HcMerchant>().eq(HcMerchant::getId, merchantId));
					if(null != one){
						merchantNames.add(one.getStoreName());
					}
				}
			}else {
				HcMerchant one = hcMerchantService.getOne(new LambdaQueryWrapper<HcMerchant>().eq(HcMerchant::getId, listingStoreIds));
				if(null != one){
					merchantNames.add(one.getStoreName());
				}
			}
			record.setListingStore(Joiner.on("，").join(merchantNames));
			//todo订单数量
			long count = hcCompanyFoodOrderService.list(new LambdaQueryWrapper<HcCompanyFoodOrder>().eq(HcCompanyFoodOrder::getDetailsId, record.getId()))
					.stream().filter(c-> !c.getState().equals(HcOrderStateEnum.CANCELED)).map(HcCompanyFoodOrder::getOrderNum).distinct().collect(Collectors.toList()).size();
			record.setOrderCount(count);

			//开始时间
			LambdaQueryWrapper<HcCompanyInfo> companyInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
			companyInfoLambdaQueryWrapper.eq(HcCompanyInfo::getId,record.getCompanyId());
			HcCompanyInfo hcCompanyInfo = hcCompanyInfoService.getOne(companyInfoLambdaQueryWrapper);
			record.setCompanyStartTime(hcCompanyInfo.getStartTime());
		}
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @return
	 */
	@AutoLog(value = "创建副本")
	@ApiOperation(value="创建副本", notes="创建副本")
	//@RequiresPermissions("org.jeecg.modules:hc_company_detail:add")
	@GetMapping(value = "/createTranscript")
	public Result<String> createTranscript(@RequestParam (name = "detailId")String detailId,
										   @RequestParam (name = "date")String date) {
		hcCompanyDetailService.createTranscript(detailId,date);
		return Result.OK("创建成功！");
	}

	/**
	 *   添加
	 *
	 * @param hcCompanyDetail
	 * @return
	 */
	@AutoLog(value = "企业详情表-添加")
	@ApiOperation(value="企业详情表-添加", notes="企业详情表-添加")
	//@RequiresPermissions("org.jeecg.modules:hc_company_detail:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody HcCompanyDetail hcCompanyDetail) {
		hcCompanyDetailService.save(hcCompanyDetail);
		return Result.OK("添加成功！");
	}

	/**
	 *   通过id删除
	 *
	 * @param merchantId
	 * @return
	 */
	@AutoLog(value = "删除商户")
	@ApiOperation(value="删除商户", notes="删除商户")
	//@RequiresPermissions("org.jeecg.modules:hc_company_detail:delete")
	@GetMapping(value = "/deleteMerchant")
	public Result<String> deleteMerchant(@RequestParam(name="merchantId") String merchantId,
										 @RequestParam(name="detailId") String detailId) {
		hcCompanyDetailService.deleteMerchant(merchantId,detailId);
		return Result.OK("删除成功!");
	}

	/**
	 *  编辑
	 *
	 * @param hcCompanyDetail
	 * @return
	 */
	@AutoLog(value = "企业详情表-编辑")
	@ApiOperation(value="企业详情表-编辑", notes="企业详情表-编辑")
	//@RequiresPermissions("org.jeecg.modules:hc_company_detail:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody HcCompanyDetail hcCompanyDetail) {
		hcCompanyDetailService.updateNew(hcCompanyDetail);
		return Result.OK("编辑成功!");
	}

	/**
	 *  编辑
	 *
	 * @param hcCompanyDetail
	 * @return
	 */
	@AutoLog(value = "企业详情表-编辑")
	@ApiOperation(value="企业详情表-编辑", notes="企业详情表-编辑")
	//@RequiresPermissions("org.jeecg.modules:hc_company_detail:edit")
	@RequestMapping(value = "/updateById", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> updateById(@RequestBody HcCompanyDetail hcCompanyDetail) {
		hcCompanyDetailService.updateById(hcCompanyDetail);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "企业详情表-通过id删除")
	@ApiOperation(value="企业详情表-通过id删除", notes="企业详情表-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:hc_company_detail:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		HcCompanyDetail hcCompanyDetail = hcCompanyDetailService.getById(id);
		//删除展示菜品
		LambdaQueryWrapper<HcCompanyFoodDisplay> displayLambdaQueryWrapper = new LambdaQueryWrapper<>();
		displayLambdaQueryWrapper.eq(HcCompanyFoodDisplay::getCompanyDetailId,hcCompanyDetail.getId());
		hcCompanyFoodDisplayService.remove(displayLambdaQueryWrapper);
		//删除详情
		hcCompanyDetailService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "企业详情表-批量删除")
	@ApiOperation(value="企业详情表-批量删除", notes="企业详情表-批量删除")
	//@RequiresPermissions("org.jeecg.modules:hc_company_detail:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.hcCompanyDetailService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "企业详情表-通过id查询")
	@ApiOperation(value="企业详情表-通过id查询", notes="企业详情表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<HcCompanyDetail> queryById(@RequestParam(name="id",required=true) String id) {
		HcCompanyDetail hcCompanyDetail = hcCompanyDetailService.getById(id);
		if(hcCompanyDetail==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hcCompanyDetail);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param hcCompanyDetail
    */
    //@RequiresPermissions("org.jeecg.modules:hc_company_detail:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HcCompanyDetail hcCompanyDetail) {
        return super.exportXls(request, hcCompanyDetail, HcCompanyDetail.class, "企业详情表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("hc_company_detail:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HcCompanyDetail.class);
    }

}
